package com.spzx.product.controller;


import com.spzx.common.core.web.controller.BaseController;
import com.spzx.common.core.web.domain.AjaxResult;
import com.spzx.common.core.web.page.TableDataInfo;
import com.spzx.common.log.annotation.Log;
import com.spzx.common.log.enums.BusinessType;
import com.spzx.common.security.annotation.RequiresPermissions;
import com.spzx.common.security.utils.SecurityUtils;
import com.spzx.product.domain.ProductUnit;
import com.spzx.product.service.ProductUnitService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * 商品单位 前端控制器
 * </p>
 *
 * @author spzx
 * @since 2024-12-19
 */
@Tag(name = "商品单位")
@RestController
@RequestMapping("/productUnit")
public class ProductUnitController extends BaseController {

    @Autowired
    private ProductUnitService productUnitService;

    /**
     * 单位列表
     * @param name
     * @return AjaxResult
     */
    @Log(title = "单位列表", businessType = BusinessType.OTHER)
    @Operation(summary = "单位列表")
    @GetMapping("/list")
    public TableDataInfo list(
            @Parameter(description = "单位名称")
            String name) {
        startPage();
        List<ProductUnit> list = productUnitService.selectBrandList(name);
        return getDataTable(list);
    }

    /**
     *  单位详情
     *
     * @param id
     * @return AjaxResult
     */
    @Log(title = "商品单位", businessType = BusinessType.OTHER)
    @Operation(summary = "单位详情")
    @GetMapping("/{id}")
    public AjaxResult getInfo(
            @Parameter(description = "单位编号", example = "null")
            @Validated
            @PathVariable("id")
            String id) {
        return success(productUnitService.getById(id));
    }

    /**
     * 新增商品
     *
     * @param productUnit
     * @return AjaxResult
     */
    @Log(title = "商品单位", businessType = BusinessType.INSERT)
    @Operation(summary = "新增单位")
    @PostMapping()
    public AjaxResult add(
            @Parameter(description = "商品", example = "新增单位信息")
            @Validated //解决前端传来的数据为空时，后端接收为null导致校验不通过的问题
            @RequestBody ProductUnit productUnit) {
        if (!productUnitService.checkProductUnitNameUnique(productUnit)) {
            return error("新增单位'" + productUnit.getName() + "'失败，商品单位已存在");
        } else {
            // 维护单位创建者信息
            productUnit.setCreateBy(SecurityUtils.getUsername());
            return toAjax(productUnitService.save(productUnit));
        }
    }

    /**
     * 单位修改
     *
     * @param productUnit
     * @return AjaxResult
     */
    @Log(title = "商品单位", businessType = BusinessType.UPDATE)
    @Operation(summary = "单位修改")
    @PutMapping()
    public AjaxResult edit(
            @Parameter(description = "商品单位")
            @Validated
            @RequestBody
            ProductUnit productUnit) {
        if (!productUnitService.checkProductUnitNameUnique(productUnit)) {
            return error("修改单位'" + productUnit.getName() + "'失败，商品单位已存在");
        } else {
            // 维护单位更新者信息
            productUnit.setUpdateBy(SecurityUtils.getUsername());
            return toAjax(productUnitService.updateById(productUnit));
        }
    }

    /**
     * 单位删除
     * @param ids
     * @return AjaxResult
     */
    @Log(title = "商品单位", businessType = BusinessType.DELETE)
    @Operation(summary = "单位删除")
    @DeleteMapping("/{ids}")
    public AjaxResult remove(
            @Parameter(description = "商品编号", example = "[1,2]")
            @PathVariable List<Long> ids) {
        return toAjax(productUnitService.removeBatchByIds(ids));
    }


    /**
     * 获取全部单位
     * @return
     */
    @Operation(summary = "获取全部单位")
    @GetMapping("/getUnitAll")
    public AjaxResult selectProductUnitAll() {
        return success(productUnitService.list());
    }
}
